/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import DTO.playlist.ArtistType;
import DTO.playlist.ArtistsType;
import DTO.playlist.PlaylistType;
import DTO.playlist.Playlists;
import DTO.playlist.SongType;
import DTO.playlist.SongsType;

/**
 *
 * @author DinhNP
 */
public class SearchPlaylistSAXHandler extends DefaultHandler {

    Playlists playlist;
    private String tagName;
    private boolean found;
    PlaylistType aPlaylist;
    private String userName;
    SongsType listOfSong;
    SongType aSong;
    ArtistType aArtist;
    ArtistsType listOfArtits;

    public SearchPlaylistSAXHandler() {
        userName = "";
        tagName = "";
        found = false;
    }

    public SearchPlaylistSAXHandler(String userName) {
        this.userName = userName;
        tagName = "";
        found = false;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

        tagName = qName;
        if (qName.equals("Playlist")) {
            aPlaylist = new PlaylistType();
        }
        if (found) {
            if (qName.equals("Songs")) {
                listOfSong = new SongsType();
            } else if (qName.equals("Song")) {
                aSong = new SongType();
            } else if (qName.equals("Artists")) {
                listOfArtits = new ArtistsType();
            } else if (qName.equals("Artist")) {
                aArtist = new ArtistType();
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (found) {
            if (qName.equals("Song")) {
                if (aSong != null) {
                    listOfSong.getSong().add(aSong);
                }
            } else if (qName.equals("Songs")) {
                if (listOfSong != null) {
                    aPlaylist.setSongs(listOfSong);
                }
            } else if (qName.equals("Playlist")) {
                if (aPlaylist != null) {
                    playlist = new Playlists();
                    playlist.getPlaylist().add(aPlaylist);
                    found = false;
                }
            }
        }

        tagName = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {


        if (tagName.equals("PlaylistID")) {
            String playlistId = new String(ch, start, length);
            aPlaylist.setPlaylistID(BigInteger.valueOf(Integer.valueOf(playlistId.trim())));
        }
        if (tagName.equals("Username")) {
            String id = new String(ch, start, length);
            if (id.trim().equals(userName.trim())) {
                aPlaylist.setPlaylistID(BigInteger.valueOf(Integer.parseInt(id.trim())));
                found = true;
            }
        }
        if (found) {
            if (tagName.equals("PlaylistName")) {
                String playlistName = new String(ch, start, length);
                aPlaylist.setPlaylistName(playlistName.trim());
            } else if (tagName.equals("DateCreated")) {

                //Get DateCreated
                try {
                    String dateCreated = new String(ch, start, length);
                    Timestamp time = Timestamp.valueOf(dateCreated.trim());
                    GregorianCalendar gcal = new GregorianCalendar();
                    gcal.setTime(time);
                    XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
                    aPlaylist.setDateCreated(xgcal);
                } catch (DatatypeConfigurationException ex) {
                    Logger.getLogger(SearchPlaylistSAXHandler.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else if (tagName.equals("ListenNumber")) {
                String listenNumber = new String(ch, start, length);
                aPlaylist.setListenNumber(BigInteger.valueOf(Integer.parseInt(listenNumber.trim())));
            } else if (tagName.equals("PlaylistCover")) {
                String playlistCover = new String(ch, start, length);
                aPlaylist.setPlaylistCover(playlistCover.trim());
            } else if (tagName.equals("PlaylistDescription")) {
                String playlistDescription = new String(ch, start, length);
                aPlaylist.setPlaylistDescription(playlistDescription.trim());
            } else if (tagName.equals("PlaylistLikeNumber")) {
                String playlistLikeNumber = new String(ch, start, length);
                aPlaylist.setPlaylistLikeNumber(BigInteger.valueOf(Integer.parseInt(playlistLikeNumber.trim())));
            } else if (tagName.equals("SongID")) {
                String songId = new String(ch, start, length);
                aSong.setSongID(BigInteger.valueOf(Integer.parseInt(songId.trim())));
            } else if (tagName.equals("SongTitle")) {
                String songTitle = new String(ch, start, length);
                aSong.setSongTitle(songTitle.trim());
            } else if (tagName.equals("SongSRC")) {
                String songSRC = new String(ch, start, length);
                aSong.setSongSRC(songSRC.trim());
            } else if (tagName.equals("Lyrics")) {
                String lyrics = new String(ch, start, length);
                aSong.setLyrics(lyrics.trim());
            } else if (tagName.equals("isMV")) {
                String isMV = new String(ch, start, length);
                aSong.setIsMV(Boolean.valueOf(isMV.trim()));
            } else if (tagName.equals("OrderNumber")) {
                String orderNumber = new String(ch, start, length);
                aSong.setOrderNumber(BigInteger.valueOf(Integer.parseInt(orderNumber.trim())));
            } else if (tagName.equals("ArtistID")) {
                String artistID = new String(ch, start, length);
                aArtist.setArtistID(BigInteger.valueOf(Integer.parseInt(artistID.trim())));
            } else if (tagName.equals("ArtistName")) {
                String artistName = new String(ch, start, length);
                aArtist.setArtistName(artistName.trim());
            }
        }
    }

    public Playlists getPlaylist() {
        return playlist;
    }
}
